<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import axios from '../axios';
import { router } from '../router'
import { AsyncValidator } from '../util/async-validator'

const formRef = ref<FormInstance>()

const formData = reactive({
    username: '',
    nickname: '',
    password: '',
})

const validateusername: AsyncValidator = async (rule, value, callback, source, opt) => {
    const res = await axios.get(`/user?username=${value}`)
    if (res.data['user'] != null) {
        callback('用户名已存在')
    }
}

const formRules = reactive<FormRules>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 4, max: 20, message: '用户名长度在4-20字符之间', trigger: 'blur' },
        { pattern: '^[0-9a-zA-Z_\\-]+$', message: '用户名仅支持字母数字和-_', trigger: 'blur' },
        { asyncValidator: validateusername, trigger: 'blur' }
    ],
    nickname: [
        { min: 2, max: 20, message: '教师姓名长度在2-20之间', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 4, max: 20, message: '密码长度在6-20字符之间', trigger: 'blur' },
    ],
})

const onSubmit = async () => {
    // 不填昵称默认与用户名一致
    if (formData.nickname == '') {
        formData.nickname = formData.username
    }
    const valid = await formRef.value?.validate()
    if (!valid) {
        return
    }

    const res = await axios.post('/register', {
        username: formData.username,
        nickname: formData.nickname,
        password: formData.password,
        role: 'teacher',
    })
    

    if (!res.data['ok']) {
        ElMessage({
            type: 'error',
            message: res.data['msg']
        })
        return
    }

    ElMessage({
        type: 'success',
        message: `注册成功`
    })

    router.push('/home')
}
</script>

<template>
<div class="outer">
<el-card>
<div class="warp">
    <h1 class="title">
      在线申请及审批系统
    </h1>
    <el-form
        ref="formRef"
        :rules="formRules"
        :model="formData"
        label-position="top" 
        style="width: 300px; margin-bottom: 20px;"
    >
        <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="教师姓名" prop="nickname">
            <el-input v-model="formData.nickname" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" @keyup.enter.native="onSubmit">
            <el-input v-model="formData.password" show-password ></el-input>
        </el-form-item>
        <el-form-item align="middle">
            <el-col>
                <div class="link">
                    <el-link type="info" href="/login">登录</el-link>
                </div>
            </el-col>
        </el-form-item>
        <el-form-item align="middle">
            <el-col>
                <el-button size="large"  @click="onSubmit" style="width: 300px;">注册</el-button>
            </el-col>
        </el-form-item>

    </el-form>
</div>
</el-card>
</div>
</template>

<style scoped>
.card {
  box-shadow: none;
}

.warp {
    display: flex;
    flex-direction: column;

    align-items: center;
    justify-content: center;
}

.link {
    display: flex;
    align-items: center;
    justify-content: end;
}

h1 {
    color: var(--el-text-color-primary);
    margin-bottom: 60px;
}
.el-card {
    display: flex;
    justify-content: center;
    align-items: center;

    width: 500px;
}

.outer {
    display: flex;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
}
</style>
